#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// 定义栈的数据结构
typedef struct Stack {
    int top;
    char *items;
} Stack;

// 创建一个新的栈
Stack *createStack(int capacity) {
    Stack *stack = (Stack *)malloc(sizeof(Stack));
    stack->top = -1;
    stack->items = (char *)malloc(capacity * sizeof(char));
    return stack;
}

// 检查栈是否为空
bool isEmpty(Stack *stack) {
    return stack->top == -1;
}

// 入栈操作
void push(Stack *stack, char item) {
    stack->items[++stack->top] = item;
}

// 出栈操作
char pop(Stack *stack) {
    if (!isEmpty(stack)) {
        return stack->items[stack->top--];
    }
    return '\0';
}

// 判断表达式中括号是否配对的函数
bool EXYX(char E[]) {
    Stack *stack = createStack(100);  // 假设表达式长度不超过 100
    int i = 0;

    while (E[i]!= '#') {
        if (E[i] == '(') {
            push(stack, E[i]);
        } else if (E[i] == ')') {
            if (isEmpty(stack)) {
                free(stack);
                return false;
            }
            pop(stack);
        }
        i++;
    }

    if (isEmpty(stack)) {
        free(stack);
        return true;
    } else {
        free(stack);
        return false;
    }
}

int main() {
    char E[] = "((a + b) * (c - d))#";
    if (EXYX(E)) {
        printf("括号配对\n");
    } else {
        printf("括号不配对\n");
    }
    return 0;
}